/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import Entity.*;
import javax.ejb.Remove;

/**
 *
 * @author User
 */
@Stateless
public class GroupBuyingManagerBean implements GroupBuyingManagerBeanRemote {

    @PersistenceContext
    private EntityManager em;
    private GroupBuyingItem groupBuy;

    // constructor
    public GroupBuyingManagerBean() {
    }

    // Add new entry of group buying items
    public String addGroupBuyingItem(
            Double originalPrice,
            Double offeredPrice,
            Date effectiveEnd,
            Date effectiveStart,
            Date redemptionEnd,
            Date redemptionStart,
            Integer minQuota,
            Integer maxQuota,
            Integer personalQuota,
            Boolean requireBooking,
            String description,
            String validateRequirements,
            String name, String stakeholderName) {
        Stakeholder s = new Stakeholder();
        s = em.find(Stakeholder.class, stakeholderName);
        if (s == null) {
            return "stakeholder is not found";
        }
        groupBuy = new GroupBuyingItem();
        try {
            groupBuy.create(originalPrice, offeredPrice, effectiveEnd, effectiveStart, redemptionEnd,
                    redemptionStart, minQuota, maxQuota, personalQuota, requireBooking, description, 
                    validateRequirements, name, s);
        } catch (Exception e) {
            return "data field missing!";
        }
        em.persist(groupBuy);
        s.addGroupBuyingItem(groupBuy); 
        em.persist(s);
        System.out.println(s.getName() + " just created a new group buying sale.");
        return "proposal successfully added";
    }

    // Search for group buying items
    public List<GroupBuyingItem> getStakeholderGBItem(String stakeholderName) {
        Query q = em.createQuery("SELECT g FROM GroupBuyingItem AS g WHERE g.stakeholder.name = :stakeholderName");
        q.setParameter("stakeholderName", stakeholderName);
        List groupons = new ArrayList();
        for (Object o : q.getResultList()) {
            GroupBuyingItem g = new GroupBuyingItem();
            g = (GroupBuyingItem) o;
            groupons.add(g);
        }
        return groupons;
    }
    
    public List<GroupBuyingItem> getGBItem(){
        Query q = em.createQuery("SELECT g FROM GroupBuyingItem as g");
        return q.getResultList();
    }
    
    public void approveProposal (Long id){
        GroupBuyingItem pbItem = em.find(GroupBuyingItem.class, id);
        if (pbItem == null)
            return;
        if (!pbItem.getStatus().equalsIgnoreCase("pending"))
            return;
        pbItem.setIsApproved(true);
        em.persist(pbItem);
    }
    
    public void denyProposal (Long id){
        GroupBuyingItem pbItem = em.find(GroupBuyingItem.class, id);
        if (pbItem == null)
            return;
        if (!pbItem.getStatus().equalsIgnoreCase("pending"))
            return;
        pbItem.setIsApproved(false);
        em.persist(pbItem);
    }
    
    public void deleteProposal (Long id){
        GroupBuyingItem item = em.find(GroupBuyingItem.class, id);
        if (item == null)
            return;
        if (!item.getStatus().equalsIgnoreCase("pending"))
            return;
        em.remove(item);
        
    }
}